package softdone;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;

import softdata.Pla_item;
import softdata.Pos_data;
import softdata.Mai_content;

public class Mai_done{
	public int Sum_Plates=0; 			//返回当前页 板块的数量
	public int Cur_Posts=0;				// 返回当前板块最多为4个帖子例子值
	public int Cur_Posts_User=0;		// 用户帖子数
	public Collection plates;
	public Collection posts=new ArrayList();
	public Mai_content maincontent=new Mai_content();
	public void Pla_Read(int page){
		String sql="{call plates_read(?)}";
		try{
			Sum_Plates=0;
			Conn dataconn=new Conn();
			plates=new ArrayList();
			dataconn.Create();
			CallableStatement cs=dataconn.CSt_SQL(sql);
			cs.setInt(1, page);
			ResultSet rs=cs.executeQuery();
			while(rs.next())
			{
				Pla_item plateitem=new Pla_item();
				plateitem.ID=rs.getInt(1);
				plateitem.Nam_Plate=rs.getString(2);
				plateitem.Num_Post=rs.getInt(3);
				plateitem.Num_Clicks=rs.getInt(4);
				plates.add(plateitem);
				plateitem=null;
				Sum_Plates++;
			}
			dataconn.CloseCC();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public void Posts_Read(int id){
		String sql="{call post_news_list4(?)}";
		try{
			Cur_Posts=0;
			Conn dataconn=new Conn();
			posts=new ArrayList();
			dataconn.Create();
			CallableStatement cs=dataconn.CSt_SQL(sql);
			cs.setInt(1, id);
			ResultSet rs=cs.executeQuery();
			while(rs.next())
			{
				Pos_data postitem=new Pos_data();
				postitem.ID_Post=rs.getInt(1);
				postitem.ID_User=rs.getInt(2);
				postitem.Num_Reply=rs.getInt(3);
				postitem.Title=rs.getString(4);
				postitem.Num_Clicks=rs.getInt(5);
				postitem.Tim_ReplyLast=rs.getString(6);
				postitem.Name_User=rs.getString(7);
				posts.add(postitem);
				Cur_Posts++;
			}
			dataconn.CloseCC();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public void Main_Content(){
		String sql="{call main_content(?,?,?,?,?)}";
		try{
			Conn dataconn=new Conn();
			dataconn.Create();
			CallableStatement cs=dataconn.CSt_SQL(sql);
			cs.registerOutParameter(1,java.sql.Types.INTEGER);
			cs.registerOutParameter(2,java.sql.Types.INTEGER);
			cs.registerOutParameter(3,java.sql.Types.INTEGER);
			cs.registerOutParameter(4,java.sql.Types.INTEGER);
			cs.registerOutParameter(5,java.sql.Types.VARCHAR);
			cs.execute();
			maincontent.num_today=cs.getInt(1);
			maincontent.num_yestoday=cs.getInt(2);
			maincontent.sum_posts=cs.getInt(3);
			maincontent.sum_user=cs.getInt(4);
			maincontent.namenews=cs.getString(5);
			dataconn.CloseCC();
		}catch(Exception e){
			e.printStackTrace();
		}

	}
	
	
	public int  Num_User_Posts(int id){
		/*
		 * 读取用户帖子数
		 */
		int Sum=0;
		String sql="{call post_sum_user(?,?)}";
		try{
			Conn dataconn=new Conn();
			dataconn.Create();
			CallableStatement cs=dataconn.CSt_SQL(sql);
			cs.setInt(1, id);
			cs.registerOutParameter(2,java.sql.Types.INTEGER);
			cs.execute();
			Sum=cs.getInt(2);
		    dataconn.CloseCC();
		}catch(Exception e){
			e.printStackTrace();
		}
		return Sum;
	}
	public void  ListByUsernew(int id){
		/*
		 * 读取个人中心 关于用户最新帖子最多4
		 */
		String sql="{call post_new_list4_user(?)}";
		try{
			posts=new ArrayList();
			Cur_Posts_User=0;
			Conn dataconn=new Conn();
			dataconn.Create();
			CallableStatement cs=dataconn.CSt_SQL(sql);
			cs.setInt(1,id);
			ResultSet rs=cs.executeQuery();
			while(rs.next())
			{
				Pos_data postitem=new Pos_data();
				postitem.ID_Post=rs.getInt(1);
				postitem.ID_User=rs.getInt(2);
				postitem.Num_Reply=rs.getInt(3);
				postitem.Title=rs.getString(4);
				postitem.Num_Clicks=rs.getInt(5);
				postitem.Tim_ReplyLast=rs.getString(6);
				postitem.Name_User=rs.getString(7);
				posts.add(postitem);
				Cur_Posts_User++;
			}
			dataconn.CloseCC();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}